package com.askyjt.videosearch.controller;


import com.askyjt.videosearch.common.constant.Constant;
import com.askyjt.videosearch.entity.Picture;
import com.askyjt.videosearch.service.PictureService;
import com.askyjt.videosearch.util.MinioUtil;
import com.askyjt.videosearch.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.InputStream;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yangjintao
 * @since 2020-11-05
 */
@Slf4j
@RestController
@RequestMapping("/admin/picture")
public class PictureController {

    @Resource
    private PictureService pictureService;

    @Resource
    private MinioUtil minioUtil;

    /**
     * 上传图片到数据库和milvus,图片存储在本地
     * @param description
     * @param file
     * @return
     */
    @PostMapping("/uploadPicture/v1")
    public Result<Boolean> uploadPicture(@RequestParam("description") String description,
                                         @RequestParam("file") MultipartFile file) {
        if (file.isEmpty() || file.getSize() == 0) {
            return Result.error("error", "文件不能为空！");
        }
        try {
            String fileName = null;
            if ((fileName = file.getOriginalFilename()) != null) {
                String newName = Constant.MINIO_PICTURE_PREFIX + UUID.randomUUID().toString().replaceAll("-", "")
                        + fileName.substring(fileName.lastIndexOf("."));
                File saveFile = new File(Constant.LOCAL_SAVE_PATH + newName);
                Picture picture = new Picture().setImageUrl(saveFile.getAbsolutePath());
                file.transferTo(saveFile);
                if (description != null) {
                    picture.setImageDescription(description);
                }
                Boolean saveResult = pictureService.savePicture(picture);
                if (saveResult) {
                    log.info("成功上传一张图片: " + newName);
                    return Result.success();
                }
                return Result.error();

            }
            return Result.error("error", "文件名不能为空！");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("error", "上传失败");
        }
    }

    /**
     * 图片存在minio服务器上
     * @param description
     * @param file
     * @return
     */
    @PostMapping("/uploadPicture/v2")
    public Result<Boolean> uploadPicture1(@RequestParam("description") String description,
                                         @RequestParam("file") MultipartFile file) {
        if (file.isEmpty() || file.getSize() == 0) {
            return Result.error("error", "文件不能为空！");
        }
        try {
            String fileName = null;
            if ((fileName = file.getOriginalFilename()) != null) {
                String newName = Constant.MINIO_PICTURE_PREFIX + UUID.randomUUID().toString().replaceAll("-", "")
                        + fileName.substring(fileName.lastIndexOf("."));
                InputStream inputStream = file.getInputStream();
                minioUtil.putObject(Constant.MINIO_BUCKET_PICTURE, newName, inputStream, file.getContentType());
                inputStream.close();
                String url = minioUtil.getObjectUrl(Constant.MINIO_BUCKET_PICTURE, newName);
                Picture picture = new Picture().setImageUrl(url);
                if (description != null) {
                    picture.setImageDescription(description);
                }
                Boolean saveResult = pictureService.savePicture(picture);
                if (saveResult) {
                    log.info("成功上传一张图片: " + newName);
                    return Result.success("success",url,true);
                }
                return Result.error();

            }
            return Result.error("error", "文件名不能为空！");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("error", "上传失败");
        }
    }

    /**
     * 通过id批量删除图片
     * @param milvusIds
     * @return
     */
    @PostMapping("/deletePicture")
    public Result<Boolean> deletePicture(Long[] milvusIds) {
        Integer deleteResult = null;
        try {
            deleteResult = pictureService.deletePicture(Arrays.asList(milvusIds));
        } catch (InterruptedException e) {
            e.printStackTrace();
            return Result.error("error","删除失败！");
        }
        if (deleteResult > 0) {
            return Result.success("success", "成功删除了" + deleteResult + "条数据",true);
        }
        return Result.error("error","删除失败！");
    }


}

